package net.quanter.samples.nacos.provider;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.ImportResource;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

@SpringBootApplication
@EnableDiscoveryClient
@ImportResource({"classpath:dubbo.xml"})
public class NacosProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosProviderApplication.class, args);
    }

    @RestController
    class EchoController {

        @Value("${spring.application.name}")
        String applicationName;

        @Value("${spring.datasource.url}")
        String datasourceUrl;

        @Value("${env}")
        String env;

        @PostConstruct
        public void init(){
            System.out.println("datasourceUrl="+datasourceUrl);
            System.out.println("env="+env);
        }

        @GetMapping(value = "/echo/{string}")
        public Map<String,String> say(
                @PathVariable String string,
                HttpServletRequest request) throws UnknownHostException {
            InetAddress address = InetAddress.getLocalHost();
            Map<String,String> map = new HashMap<>();
            map.put("微服务名",applicationName);
            map.put("数据库连接地址",datasourceUrl);
            map.put("env",env);
            map.put("服务端ip地址",address.getHostAddress());
            map.put("请求段ip地址",request.getRemoteAddr());
            return map;
        }
    }
}

